﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web;
using System.Configuration;
using INNOVACALL.Framework.Common.Properties;

namespace INNOVACALL.Framework.Common.MVCHelpers
{
	/// <summary>
	/// Modes de gestion du cache du navigateur.
	/// </summary>
	public enum enCacheMode
	{
		/// <summary>
		/// See http://stackoverflow.com/questions/1160105/asp-net-mvc-disable-browser-cache
		/// Following values must be stored in AppSettings.config
		/// ExpirationDate (int: days), 
		/// ValidUntilExpires (true, false), 
		/// Revalidation (AllCaches, ProxyCaches, None)
		/// Cacheability (NoCache, Private, Server, ServerAndNoCache, Public, ServerAndPrivate)
		/// </summary>
		ConfigSource,
		/// <summary>
		/// Cache desactivé.
		/// </summary>
		NoCache,
		/// <summary>
		/// Navigateur par default.
		/// </summary>
		CacheEnabled
	}

	/// <summary>
	/// ASP.net Controllers / View cache management
	/// </summary>
	public class CacheManager : ActionFilterAttribute
	{
		private string eMethodBase = "INNOVACALL.Framework.Common.MVCHelpers";
		public DateTime ExpirationDate { get; set; }
		public bool ValidUntilExpires { get; set; }
		public HttpCacheRevalidation Revalidation { get; set; }
		public HttpCacheability Cacheability { get; set; }

		public CacheManager(enCacheMode mode)
			: base()
        {
            object eParameters = new { @mode = mode.ToString()};
			string ExpirationWebConfig = String.Empty;
            switch (mode)
            {
                case enCacheMode.ConfigSource:
                    ExpirationWebConfig = ConfigurationManager.AppSettings["ControllerCacheExpirationTimout"];
                    DateTime currentDate;
                    if (!DateTime.TryParse(ExpirationWebConfig, out currentDate)) {
						throw new InnovacallExceptions.CommonException(eMethodBase + ".CacheManagerConstructor",eParameters,Resources.ErrMsg_MVCHelpers_ConfigFileExpirationDateNotValid);											
					}
                    ExpirationDate = currentDate;
                    break;
                case enCacheMode.NoCache:
                    ExpirationDate = DateTime.UtcNow.AddDays(-1);
                    ValidUntilExpires = false;
                    Revalidation = HttpCacheRevalidation.AllCaches;
                    Cacheability = HttpCacheability.NoCache;
                    break;
                case enCacheMode.CacheEnabled:
                    break;
            }
        }

		/// <summary>
		/// see http://msdn.microsoft.com/en-us/library/dd381609.aspx
		/// see http://stackoverflow.com/questions/1160105/asp-net-mvc-disable-browser-cache
		/// </summary>
		/// <param name="filterContext"></param>
		public override void OnResultExecuting(ResultExecutingContext filterContext)
		{
			filterContext.HttpContext.Response.Cache.SetExpires(this.ExpirationDate);
			filterContext.HttpContext.Response.Cache.SetValidUntilExpires(this.ValidUntilExpires);
			filterContext.HttpContext.Response.Cache.SetRevalidation(this.Revalidation);
			filterContext.HttpContext.Response.Cache.SetCacheability(this.Cacheability);
			if (Cacheability == HttpCacheability.NoCache)
			{
				filterContext.HttpContext.Response.Cache.SetNoStore();
			}

			base.OnResultExecuting(filterContext);
		}
	}
}
